package com.gzw.dms.activity;

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.gzw.dms.MyApplication;
import com.gzw.dms.R;
import com.gzw.dms.adapter.BackGVAdapter;
import com.gzw.dms.adapter.MainGVAdapter;
import com.gzw.dms.base.BaseActivity;
import com.gzw.dms.bean.DriverInfo;
import com.gzw.dms.bean.OrderNumberInfo;
import com.gzw.dms.bean.UserInfo;
import com.gzw.dms.utils.HttpUtils;
import com.gzw.dms.utils.JsonUtil;
import com.gzw.dms.utils.T;
import com.gzw.dms.utils.UrlUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BackActivity extends BaseActivity {
    private Toolbar toolbar;
    private Context mContext;
    private GridView gv;
    private List<String> lsFuncs;
    private List<OrderNumberInfo> lsMapInfo = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_back);

    }

    @Override
    protected void initViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        gv = (GridView) findViewById(R.id.gv);


    }

    @Override
    protected void initData() {
        mContext = this;
        toolbar.setTitle("逆向物流");
        GetMenu getMenu = new GetMenu();
        getMenu.execute();

        UserInfo userInfo = MyApplication.getUserInfo();
        String userid = userInfo.getFlag();
        if (userid.equals("0")){

        }else if (userid.equals("1")){
        }else if (userid.equals("2")){

        }else {


        }
    }

    @Override
    protected void initEvents() {
        toolbar.setNavigationOnClickListener(new MyOnClick());
        gv.setOnItemClickListener(new myOnItemClick());


    }

    class MyOnClick implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent mIntent = new Intent();

            switch (v.getId()) {

                default:
                    Intent mIntent3 = new Intent();
                    mIntent3.putExtra("result", 1);
                    //设置返回数据
                    setResult(3, mIntent);
                    finish();
                    break;
            }
        }
    }


    private void getView() {
        BackGVAdapter adapter = new BackGVAdapter(mContext, lsFuncs, lsMapInfo);
        gv.setAdapter(adapter);
    }


    class myOnItemClick implements AdapterView.OnItemClickListener {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String b = parent.toString();
            Intent mIntent = new Intent();
            switch (lsFuncs.get(position)) {
                case "14":
                    mIntent.putExtra("fpstste", "2");
                    mIntent.setClass(mContext, BackOrderActivity.class);
                    startActivityForResult(mIntent, 1);
                    break;
                case "13":
                    mIntent.putExtra("type", "3");
                    mIntent.setClass(mContext,SelectDriverActivity.class);
                    startActivityForResult(mIntent, 3);

                    break;
                case "12":
                    mIntent.putExtra("fpstste", "1");
                    mIntent.setClass(mContext, BackjdActivity.class);
                    startActivityForResult(mIntent, 1);

                    break;
                case "16":
                    mIntent.putExtra("fpstste", "2");
                    mIntent.setClass(mContext, BackqsActivity.class);
                    startActivityForResult(mIntent, 1);
                    break;
                case "15":
                    mIntent.putExtra("fpstste", "2");
                    mIntent.setClass(mContext, BackOrderListActivity.class);
                    startActivityForResult(mIntent, 1);
                    break;

            }
        }
    }


    public class GetMenu extends AsyncTask<DriverInfo, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(DriverInfo... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            map.put("type", "2");
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetMenuAndRole"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    final List<String> lsMap = JsonUtil.getPersons(successMsg, new TypeToken<List<String>>() {
                    }.getType());
                    lsFuncs = lsMap==null?new ArrayList<>():lsMap;
                    getView();

                    SignShipOrder getMenu = new SignShipOrder();
                    getMenu.execute();

                }
            }
        }

        @Override
        protected void onCancelled() {


        }
    }

    public class SignShipOrder extends AsyncTask<DriverInfo, Void, String> {
        //onPreExecute方法用于在执行后台任务前做一些UI操作
        @Override
        protected void onPreExecute() {

        }

        protected String doInBackground(DriverInfo... params) {
            Map<String, Object> map = new HashMap<>();
            UserInfo userInfo = MyApplication.getUserInfo();
            map.put("userInfo", userInfo);
            String json = JsonUtil.toJson(map);
            return HttpUtils.doPost(UrlUtils.getInstance().getJavaUrl("appGetReverseShipperOrderCountInfo"), "json=" + json);
        }

        @Override
        protected void onPostExecute(final String result) {
            if (isNull(result)) {
                T.showShort(mContext, "连接服务器异常");
            } else {

                if (result.indexOf("errorMsg") > 0) {
                    String errorMsg = JsonUtil.getMsg(result, "errorMsg");
                    T.showShort(mContext, errorMsg);
                } else {
                    String successMsg = JsonUtil.getMsg(result, "successMsg");
                    final List<OrderNumberInfo> lsMap = JsonUtil.getPersons(successMsg, new TypeToken<List<OrderNumberInfo>>() {
                    }.getType());
                    lsMapInfo = lsMap;
                    getView();

                }
            }
        }

        @Override
        protected void onCancelled() {


        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {


        if (requestCode == 3) {
            SignShipOrder signShipOrder = new SignShipOrder();
            signShipOrder.execute();
        }

    }
}